﻿// sandbox.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sandbox.h"

#include <algorithm>
#include <functional>
#include <locale>
#include <string>

#include <sstream>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// The one and only application object

CWinApp theApp;

using namespace std;

#include <mbctype.h>
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// initialize MFC and print and error on failure
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO: change error code to suit your needs
			_tprintf(_T("Fatal Error: MFC initialization failed\n"));
			nRetCode = 1;
		}

		else
		{
			int cp = _getmbcp();
			int mbcpret = _setmbcp(_MB_CP_LOCALE);
			// Create and display a C style string, and then use it 
			// to create different kinds of strings.
			std::string orig = "abcdのアドバイスefgh한글ijkl韓文";
			cout << orig.c_str() << " (char *)" << endl;

			CStringA cstrA("abcdのアドバイスefgh한글ijkl韓文");
			CStringW cstrW(L"abcdのアドバイスefgh한글ijkl韓文");
			CStringW wfromcnv = CA2W(cstrA);

			char* abuf = cstrA.GetBuffer();
			wchar_t* wbuf = cstrW.GetBuffer();
			wchar_t* cbuf = wfromcnv.GetBuffer();

			unsigned char* chp = (unsigned char*)abuf;
			unsigned char* buf = (unsigned char*)abuf;
			int cumax = MB_CUR_MAX;
			int gcp = _getmbcp();
			char ch0 = chp[0];
			while (ch0 != '\0')
			{
				int dbg = 0;

				wchar_t ww(*chp);

				size_t len = _mbclen(chp/*, _create_locale(LC_CTYPE, "kor")*/);
				wchar_t wch;


				int ret = mbtowc(&wch, (char*)chp, len);

				chp = _mbsinc(buf);
				ch0 = chp[0];

				buf = chp;
			}
			

			int astrLen = cstrA.GetLength();
			int wstrLen = cstrW.GetLength();
			int cstrLen = cstrW.GetLength();


			// newsize describes the length of the 
			// wchar_t string called wcstring in terms of the number 
			// of wide characters, not the number of bytes.
			size_t newsize = strlen(orig.c_str()) + 1;

			// The following creates a buffer large enough to contain 
			// the exact number of characters in the original string
			// in the new format. If you want to add more characters
			// to the end of the string, increase the value of newsize
			// to increase the size of the buffer.
			wchar_t * wcstring = new wchar_t[newsize];

			// Convert char* string to a wchar_t* string.
			size_t convertedChars = 0;
			mbstowcs_s(&convertedChars, wcstring, newsize, orig.c_str(), _TRUNCATE);
			// Display the result and indicate the type of string that it is.
			wcout << wcstring << _T(" (wchar_t *") << convertedChars << _T(")") << endl;

			std::wstring wstr(wcstring);
			wcout << wstr.c_str() << _T(" (wchar_t */from wstring)") << endl;

			std::wstring orw(L"abcdのアドバイスefgh한글ijkl韓文");

			std::wstringstream ws;
			ws << orig.c_str();

			std::wstring wsstr(ws.str());

			if (wstr == orw)
			{
				int dbg = 0;
			}

			

			// Convert the C style string to a _bstr_t string.
			//_bstr_t bstrt(orig);
			//// Append the type of string to the new string
			//// and then display the result.
			//bstrt += " (_bstr_t)";
			//cout << bstrt << endl;

			// Convert the C style string to a CComBSTR string.
			//CComBSTR ccombstr(orig);
			//if (ccombstr.Append(_T(" (CComBSTR)")) == S_OK)
			//{
			//	CW2A printstr(ccombstr);
			//	cout << printstr << endl;
			//}

			//// Convert the C style string to a CstringA and display it.
			//CStringA cstringa(orig);
			//cstringa += " (CStringA)";
			//cout << cstringa << endl;

			//// Convert the C style string to a CStringW and display it.
			//CStringW cstring(orig);
			//cstring += " (CStringW)";
			//// To display a CStringW correctly, use wcout and cast cstring
			//// to (LPCTSTR).
			//wcout << (LPCTSTR)cstring << endl;

			//// Convert the C style string to a basic_string and display it.
			//string basicstring(orig);
			//basicstring += " (basic_string)";
			//cout << basicstring << endl;

			//// Convert the C style string to a System::String and display it.
			//String ^systemstring = gcnew String(orig);
			//systemstring += " (System::String)";
			//Console::WriteLine("{0}", systemstring);
			//delete systemstring;

			//TextFile tf = util::createTextFile(_T("d:\\text-ansi.txt"));
			//tf->openToRead();

			//tstring tstr;
			//tf->readLine(tstr);

			//AfxMessageBox(tstr.c_str());

			//int dbg = 0;
		}
	}
	else
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
		nRetCode = 1;
	}

	return nRetCode;
}

//
//int main() 
//{	
//}